package com.mbengue.corp.artisan.vue.fenetre;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.mbengue.corp.artisan.controleur.Controleur;
import com.mbengue.corp.artisan.modele.Client;
import com.mbengue.corp.artisan.utils.Outil;

public class AjoutClient extends JFrame {
  private static final long serialVersionUID = 359773337724295957L;
  private final Controleur controleur;
  private final JTable tableau;
  private final JButton nouvelle;
  private final JButton valider;
  private final JButton supprimer;
  private int nbClients = 0;

  public AjoutClient(Controleur controleur) {
    super("Ajouter De Nouveaux Clients");
    this.setSize(400, 300);
    this.setLocationRelativeTo(null);
    this.controleur = controleur;
    this.tableau = new JTable();
    this.nouvelle = new JButton("Nouveau");
    this.valider = new JButton("Valider");
    this.valider.setEnabled(false);
    this.supprimer = new JButton("Supprimer");
    this.supprimer.setEnabled(false);

    this.initTableau();
    this.ajouterEcouteursBoutons();
    JPanel pan = new JPanel();
    this.getContentPane().add(new JScrollPane(this.tableau), BorderLayout.CENTER);
    pan.add(this.nouvelle, BorderLayout.WEST);
    pan.add(this.supprimer, BorderLayout.CENTER);
    pan.add(this.valider, BorderLayout.EAST);
    pan.setVisible(true);
    this.getContentPane().add(pan, BorderLayout.SOUTH);
    this.setVisible(true);
  }

  private void initTableau() {
    this.tableau.setModel(new DefaultTableModel(Outil.EN_TETE_CLIENT, nbClients));
    this.tableau.repaint();
  }

  private void ajouterEcouteursBoutons() {
    this.nouvelle.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        ((DefaultTableModel) tableau.getModel()).addRow(new String[] { "", "", "", "", "", "" });
        nbClients++;
        tableau.repaint();
        tableau.setVisible(true);
        valider.setEnabled(true);
        supprimer.setEnabled(true);
      }
    });

    this.supprimer.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        int index = tableau.getSelectedRow();
        if (index >= 0) {
          ((DefaultTableModel) tableau.getModel()).removeRow(index);
          --nbClients;
          tableau.repaint();
          tableau.setVisible(true);
        }
      }
    });

    this.valider.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e2) {
        System.out.println("Nombre de nouveaux clients ajoutés: " + nbClients);
        if (nbClients > 0) {
          try {
            TableModel model = getTableau().getModel();
            List<Client> clients = new ArrayList<Client>();
            for (int index = 0; index < nbClients; index++) {
              Client client = new Client();
              client.setNom((String) model.getValueAt(index, 0));
              client.setPrenom((String) model.getValueAt(index, 1));
              client.setTelephone((String) model.getValueAt(index, 2));
              client.setAdresse((String) model.getValueAt(index, 3));
              client.setCodePostal((String) model.getValueAt(index, 4));
              client.setVille((String) model.getValueAt(index, 5));
              client.setFullName(client.getNom() + "-" + client.getPrenom());
              clients.add(client);
            }
            controleur.ajouterClients(clients);
            setVisible(false);
            JOptionPane.showMessageDialog(null, "La sauvegarde des nouveaux clients a été un succès.", "Information",
                JOptionPane.INFORMATION_MESSAGE);
          } catch (Exception exc) {
            exc.printStackTrace();
            JOptionPane.showMessageDialog(null, "Une erreur est survenu lors de la sauvegarde.", "Erreur",
                JOptionPane.ERROR_MESSAGE);
          }
        } else {
          JOptionPane.showMessageDialog(null, "Il faut entrer au moins un client ou fermer la fenetre.", "Attention",
              JOptionPane.WARNING_MESSAGE);
        }
      }
    });
  }

  public int getNbClients() {
    return nbClients;
  }

  public JButton getNouvelle() {
    return nouvelle;
  }

  public JButton getSupprimer() {
    return supprimer;
  }

  public JButton getValider() {
    return valider;
  }

  public JTable getTableau() {
    return tableau;
  }
}
